package org.shopen.ai.puremvc.controller.project
{
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.puremvc.as3.utilities.statemachine.StateMachine;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.model.StateProxy;

	public class HandleNavigationRequestCommand extends SimpleCommand
	{
		override public function execute(notification:INotification):void
		{
			var requestType : String = notification.getType();
			switch (requestType)
			{
				case ApplicationConstants.PROJECT_NEXT_STEP:
					handleNextRequest(notification);
					break;
				case ApplicationConstants.PROJECT_PREV_STEP:
					handlePrevRequest(notification);
					break;
			}
		}
		
		protected function get currentState() : String
		{
			var proxy : StateProxy = facade.retrieveProxy(StateProxy.NAME) as StateProxy;
			return proxy.currentState;
		}
		
		protected function handlePrevRequest(notification : INotification) : void
		{
			// P R E V  Button
			var body : Object = notification.getBody();
			switch (currentState)
			{
				case ApplicationConstants.STATE_PROJECT_SETUP:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_PREV);
					break;
				case ApplicationConstants.STATE_PROJECT_SEARCH_STOPPED:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_PREV);
					break;
				case ApplicationConstants.STATE_PROJECT_STATISTIC:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_PREV);
					break;
			}
		}
		
		protected function handleNextRequest(notification : INotification) : void
		{
			// N E X T  Button
			var body : Object = notification.getBody();
			switch (currentState)
			{
				case ApplicationConstants.STATE_PROJECT_PREVIEW:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_NEXT);
					break;
				case ApplicationConstants.STATE_PROJECT_SETUP:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_NEXT);
					break;
				case ApplicationConstants.STATE_PROJECT_SEARCHING:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_ABORT);
					break;
				case ApplicationConstants.STATE_PROJECT_STATISTIC:
					sendNotification(StateMachine.ACTION, body, ApplicationConstants.ACTION_TRY_AGAIN);
					break;
			}
		}
	}
}